package com.woniu.supermarket.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Promotion;
import com.woniu.supermarket.mapper.PromotionMapper;
import com.woniu.supermarket.service.IPromotionService;
import com.woniu.supermarket.vo.PromotionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PromotionServiceImpl implements IPromotionService {

    @Autowired
    PromotionMapper promotionMapper;

    @Override
    public PageInfo<PromotionVo> queryByPage(Integer now, Integer size, String code, int status) {
        PageHelper.startPage(now,size);
        List<PromotionVo> lists = promotionMapper.selectPromotionByPage(code,status);
        for(PromotionVo p:lists){
            //填充促销时间段数据
            Date promotionStartTime = p.getPromotionStartTime();
            Date promotionEndTime = p.getPromotionEndTime();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            String transformDate1=simpleDateFormat.format(promotionStartTime);
            String transformDate2=simpleDateFormat.format(promotionEndTime);
            p.setPromotionTime(transformDate1+"~"+transformDate2);
            /*
            * 关于促销的状态
            *生成当前时间now
            * 当促销状态不为3（已手动终止时）
            * now与开始促销时间和结束促销时间做比较
            * now<开始，即 促销状态为2（未促销）
            * now>结束，即 促销状态为0（已促销）
            * 开始<now<结束 即 促销状态为1（促销中）
            */
            Date date = new Date();
            int promotionStatus = p.getPromotionStatus();
            if(promotionStatus != 3){
                //date在End之后
                if(date.compareTo(promotionEndTime)>0){
                    p.setPromotionStatus(0);
                }else if(date.compareTo(promotionStartTime)>0){//date在start之后
                    p.setPromotionStatus(1);
                }else{
                    p.setPromotionStatus(2);
                }
                if(p.getStatus()==0){
                    p.setPromotionStatus(2);
                }
            }
            /*审核时间  未审核时，将审核时间设为null*/
            if(p.getStatus()==0){
                p.setPromotionCheckDate(null);
            }

            //PromotionVo对象转成Promotion对象
            Promotion promotion = new Promotion();
            promotion.setId(p.getId());
            promotion.setPromotionNumber(p.getPromotionNumber());
            promotion.setPromotionType(p.getPromotionType().getId());
            promotion.setPromotionShop(p.getPromotionShop().getId());
            promotion.setPromotionStartTime(p.getPromotionStartTime());
            promotion.setPromotionEndTime(p.getPromotionEndTime());
            promotion.setPromotionStaff(p.getPromotionStaff());
            promotion.setPromotionCheckDate(p.getPromotionCheckDate());
            promotion.setPromotionStatus(p.getPromotionStatus());
            promotion.setStatus(p.getStatus());
            promotionMapper.updateByPrimaryKey(promotion);
        }
        return new PageInfo<>(lists);
    }

    @Override
    public int add(Promotion promotion) {
        return promotionMapper.insert(promotion);
    }

    @Override
    public void remove(int id) {
        promotionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void modify(Promotion promotion) {
        promotionMapper.updateByPrimaryKey(promotion);
    }

    @Override
    public Promotion queryById(Integer promotionId) {
        return promotionMapper.selectByPrimaryKey(promotionId);
    }
}
